package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Inputbill;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.MyPopupWindowListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


/**
 * Created by hacker on 2017/7/2.
 */

public class SelectInputAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Inputbill> mInputList;
    private Context mContent;
    private MyPopupWindowListener myPopupWindowListener;

    public SelectInputAdapter(List<Inputbill> list, Context content, MyPopupWindowListener listener) {
        this.mInputList = list;
        this.mContent = content;
        this.myPopupWindowListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if (mInputList.size() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 1) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.select_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.bindViewData(mInputList.get(position));
            viewHolder.setViewListener(mInputList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        if (mInputList.size() > 0) {
            return mInputList.size();
        } else {
            return 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {


        TextView plateNumberTV;


        public ViewHolder(View itemView) {
            super(itemView);
            plateNumberTV = (TextView) itemView.findViewById(R.id.plate_number);

        }

        public void setViewListener(final Inputbill input) {
            plateNumberTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    plateNumberTV.setVisibility(View.GONE);
                    String temp = null;
                    JSONObject object = new JSONObject();
                    try {
                        object.put("id", input.getId()+"");
                        object.put("contractId", input.getContractid()+"");
                        object.put("contractNumber", input.getCtnum());
                        object.put("number",input.getIbnum());
                        object.put("carNumber",input.getCarnumber());
                        object.put("weight",input.getInputnetweight()+"");
                    } catch (JSONException e) {
                        myPopupWindowListener.OnMyClick(Constant.SELECT_TYPE_INPUT, -1, "");
                        e.printStackTrace();
                    }
                    temp = object.toString();
                    myPopupWindowListener.OnMyClick(Constant.SELECT_TYPE_INPUT,
                            Integer.valueOf(input.getId() + ""), temp);

                }
            });
        }

        public void bindViewData(final Inputbill input) {
            String temp = null;
            temp = TimeUtils.parseDateTime(input.getAddtime())
                    + "\n" + input.getIbnum() + "  "
                    + input.getCarnumber() + "  " + input.getInputnetweight() + "吨";

            plateNumberTV.setText(temp);
        }
    }
}
